package io.gitee.kotle.android.utils.glide;

import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.google.android.renderscript.Range2d;
import com.google.android.renderscript.Toolkit;

import java.nio.charset.Charset;
import java.security.MessageDigest;

/**
 * 描述：
 *
 * @author jiale.wei
 * <p>
 * 创建时间：2022/8/1 10:09
 */
public class GlideBlurTransformation extends BitmapTransformation {

    private static String STRING_CHARSET_NAME = "UTF-8";
    private static final String ID = "com.kevin.glidetest.BlurTransformation";
    private static Charset CHARSET = Charset.forName(STRING_CHARSET_NAME);
    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
    private int mRadius;
    private Range2d mRange2d;

    public GlideBlurTransformation() {
        this(10);
    }

    public GlideBlurTransformation(int radius) {
        this(radius, null);
    }

    public GlideBlurTransformation(int radius, Range2d range2d) {
        this.mRadius = radius;
        this.mRange2d = range2d;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int
            outWidth, int outHeight) {
        Bitmap bitmap = toTransform;
        if (mRange2d != null) {
            bitmap = Toolkit.INSTANCE.blur(bitmap, mRadius, mRange2d);
        } else {
            bitmap = Toolkit.INSTANCE.blur(bitmap, mRadius);
        }
        return bitmap;
    }

    @Override
    public int hashCode() {
        return ID.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof GlideBlurTransformation;
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {
        messageDigest.update(ID_BYTES);
    }
}
